package com.dzu.easy;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author ZhaoDong
 * @date 2023/2/6 9:59
 * @description 290. 单词规律
 */
public class Test290 {
    public static void main(String[] args) {
        System.out.println(wordPattern("abba", "dog cat cat dog"));
        System.out.println(wordPattern("abba", "dog cat cat fish"));
        System.out.println(wordPattern("abba", "dog dog dog dog"));
    }

    public static boolean wordPattern(String pattern, String s) {

        String[] s1 = s.split(" ");
        if (s1.length != pattern.length()) {
            return false;
        }

        Map<Object, Integer> map = new HashMap<>();

        for (int i = 0; i < s1.length; i++) {
            if (!Objects.equals(map.put(pattern.charAt(i), i), map.put(s1[i], i))) {
                return false;
            }

        }
        return true;

    }
}
